/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.crud.signalFrequency;

import java.awt.Frame;
import java.math.BigDecimal;
import java.util.Calendar;
import javax.swing.JDialog;
import systole.domain.signals.SignalFrequency;
import systole.exceptions.ExceptionDAO;
import systole.utils.ImageUtils;
import systole.view.crud.controller.ControllerEntityEdition;

/**
 *
 * @author jmj
 */
public class ControllerSignalFrequencyEdition extends ControllerEntityEdition {

    /**
     */
    public ControllerSignalFrequencyEdition() {
        super();
        this.entityName = "Período de Muestreo";
    }

    @Override
    protected void loadEntityOnForm() {
        JDialogSignalFrequency formToLoad = (JDialogSignalFrequency) this.editForm;
        SignalFrequency signalFrequencyToLoad = (SignalFrequency) this.curretnEntity;
        formToLoad.getjEdtFrequency().setText(signalFrequencyToLoad.getFrequency().toString());
        formToLoad.getjEdtDescription().setText(signalFrequencyToLoad.getDescription());
    }

    @Override
    protected void loadEntityFromForm() {
        SignalFrequency signalFrequencyToLoad = (SignalFrequency) this.curretnEntity;
        JDialogSignalFrequency formToLoad = (JDialogSignalFrequency) this.editForm;
        signalFrequencyToLoad.setDescription(formToLoad.getjEdtDescription().getText());
        String value = formToLoad.getjEdtFrequency().getText();
        value = value.replace(",", ".");
        signalFrequencyToLoad.setFrequency(new BigDecimal(value));
        signalFrequencyToLoad.setLastUpdate(Calendar.getInstance());
    }

    @Override
    protected String valid() {
        JDialogSignalFrequency formToValid = (JDialogSignalFrequency) this.editForm;
        SignalFrequency signalFrequency = (SignalFrequency) this.curretnEntity;

        if ((formToValid.getjEdtDescription().getText() == null) || (formToValid.getjEdtDescription().getText().isEmpty())) {
            return "Debe ingresar la descripción";
        }

        try {
            if (this.facadeDB.getSignalFrequencyBroker().existSignalFrequency(formToValid.getjEdtDescription().getText(), signalFrequency.getId())) {
                return "Ya existe un período de muestreo con el nombre ingresado";
            }

        } catch (ExceptionDAO ex) {
            this.facadeDB.refreshSession();
            return ex.getMessage();
        }


        if ((formToValid.getjEdtFrequency().getText() == null) || (formToValid.getjEdtFrequency().getText().isEmpty())) {
            return "Debe ingresar un período de muestreo";
        }

        String value = formToValid.getjEdtFrequency().getText();
        value = value.replace(",", ".");
        try {
            @SuppressWarnings("unused")
            Double period = new Double(value);
        } catch (NumberFormatException e) {
            return "Debe ingresar un período de muestreo válido";
        }

        return null;
    }

    @Override
    protected void save() throws ExceptionDAO {
            if (this.isEditing()){
                this.facadeDB.getSignalFrequencyBroker().update((SignalFrequency) this.curretnEntity);
            } else {
                this.facadeDB.getSignalFrequencyBroker().insert((SignalFrequency) this.curretnEntity);
            }
    }

    @Override
    protected void loadIconOnForm() {
        this.editForm.setIconImage(ImageUtils.buildImage("resources/icons/medic/frequency16.png"));
    }

    @Override
    protected Object createEntity() throws ExceptionDAO {
        return new SignalFrequency();
    }

    @Override
    protected JDialog createEditionForm(Frame parent) {
        return new JDialogSignalFrequency(parent, this);
    }
}
